<?php

namespace App\Rules;

use Illuminate\Contracts\Validation\Rule;

class Interval implements Rule
{
    /**
     * Create a new rule instance.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
     * Determine if the validation rule passes.
     *
     * @param  string  $attribute
     * @param  mixed  $value
     * @return bool
     */
    public function passes($attribute, $value)
    {
        $args = explode(',', $value);
        if (count($args) !== 2)
            return false;
        if (!is_numeric($args[0]) || !is_numeric($args[1]))
            return false;
        $from =strtotime($args[0]);
        $to = strtotime($args[1]);
        if ($from > $to)
            return false;
        return true;
    }

    /**
     * Get the validation error message.
     *
     * @return string
     */
    public function message()
    {
        return ':attribute illegal interval pattern, should be pattern like 0,1';
    }
}
